package com.zby.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.admin.mapper.CategoryMapper;
import com.zby.admin.mapper.CategoryTypeMapper;
import com.zby.admin.pojo.Category;
import com.zby.admin.pojo.CategoryType;
import com.zby.admin.pojo.dto.CategoryTypeDto;
import com.zby.admin.pojo.vo.CategoryTypeVo;
import com.zby.admin.pojo.vo.GetCategoryTypeVo;
import com.zby.admin.service.CategoryTypeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryTypeServiceImp extends ServiceImpl<CategoryTypeMapper, CategoryType> implements CategoryTypeService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private CategoryTypeMapper categoryTypeMapper;
    @Override
    public boolean insert(CategoryTypeDto categoryTypeDto, HttpServletRequest request) {
        if(categoryTypeDto.getTypeName()== null){
            throw new RuntimeException("请输入属性名称");
        }
        QueryWrapper<CategoryType> categoryTypeQueryWrapper = new QueryWrapper<>();
        QueryWrapper<CategoryType> typeName = categoryTypeQueryWrapper.eq("category_type_name", categoryTypeDto.getTypeName());
        CategoryType one = this.getOne(typeName);
        if(one != null){
            throw new RuntimeException("属性名称已存在");
        }
        if(categoryTypeDto.getParentId() != 0L){
            Category category = categoryMapper.selectById(categoryTypeDto.getParentId());
            if (category == null){
                throw new RuntimeException("请输入正确的父级分类ID");
            }
        }

        CategoryType categoryType = new CategoryType();
        categoryType.setCategoryTypeName(categoryTypeDto.getTypeName());
        categoryType.setCategoryId(categoryTypeDto.getParentId());
        return this.save(categoryType);
    }



    @Override
    public boolean updateCategoryType(CategoryTypeDto categoryTypeDto, HttpServletRequest request) {
        if (categoryTypeDto.getId() == 0L){
            throw new RuntimeException("id不能为空");
        }
        if (categoryTypeDto.getTypeName() == null){
            throw new RuntimeException("分类名称不能为空");
        }
        if (categoryTypeDto.getParentId() == 0L){
            throw new RuntimeException("父级id不能为空");
        }
        if(categoryTypeDto.getParentId() != 0L){
            Category category = categoryMapper.selectById(categoryTypeDto.getParentId());
            if (category == null){
                throw new RuntimeException("请输入正确的父级分类ID");
            }
        }
        CategoryType categoryType = new CategoryType();
        categoryType.setId(categoryTypeDto.getId());
        categoryType.setCategoryTypeName(categoryTypeDto.getTypeName());
        categoryType.setCategoryId(categoryTypeDto.getParentId());
        return this.updateById(categoryType);
    }

    @Override
    public Page<CategoryTypeVo> getCategoryTypeList(Integer page, Integer pageSize) {
        page=(page-1)*pageSize;
       List<CategoryTypeVo> categoryTypeVos = categoryTypeMapper.list(page,pageSize);

       Page<CategoryTypeVo> pageInfo = new Page<>(page,pageSize);
       pageInfo.setTotal(categoryTypeMapper.selectCount(null));
       pageInfo.setRecords(categoryTypeVos);
       return pageInfo;
    }

    @Override
    public List<GetCategoryTypeVo> getCategoryType() {
        List<GetCategoryTypeVo> categoryTypeVos = categoryTypeMapper.getCategoryType();

        return categoryTypeVos;
    }
}
